/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.topic.impl;

import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.constant.DictionaryStaticValues;
import org.grayrabbit.cms.dao.topic.TopicDao;
import org.grayrabbit.cms.entity.Topic;
import org.grayrabbit.cms.util.HqlUtil;
import org.grayrabbit.cms.vo.topic.TopicBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>Topic</b> dao implementation 





 * @version 1.0，创建时间：2011-05-16 15:59:25 
 */
@Repository
public class TopicDaoImpl extends GenericHibernateDao<Topic, String>
		implements TopicDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From Topic as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see TopicDao#assembly(TopicBean)
	 */
	public QueryCondition assembly(TopicBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see TopicDao#assemblyCount(TopicBean)
	 */
	public QueryCondition assemblyCount(TopicBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see TopicDao#count(TopicBean)
	 */
	public int count(TopicBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}
	
	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see TopicDao#assemblyNameQuery(TopicBean)
	 */
	public QueryCondition assermblyNameQuery(TopicBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see TopicDao#assemblyNameQueryCount(TopicBean)
	 */
	public QueryCondition assemblyNameQueryCount(TopicBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see TopicDao#findByPage(Page, TopicBean)
	 */
	public List<Topic> findByPage(Page<Topic> page,
			TopicBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		qc.append(" order by obj.isEnable desc");
		return findByQc(qc);
	}
	/**
	 * 根据站点id查询专题列表
	 */
	public List<Topic> findBySiteId(String siteId){
		QueryCondition qc = assembly(null);
		qc.add("And obj.site.siteId = ? ", siteId);
		return findByQc(qc);
	}
	
	public List<Topic> findPublishBySiteId(String siteId,int currentTopicListIndex,int max){
		QueryCondition qc = assembly(null);
		qc.add("And obj.site.siteId = ? ", siteId);
		qc.append("And (obj.topicStatus = 3 Or obj.topicStatus = 1) ");
		qc.append("Order by obj.topicId asc");
		qc.setOffset(currentTopicListIndex);
		qc.setMaxResults(max);
		return findByQc(qc);
	}
	
	// 业务查询主要逻辑代码
	private void transNameQuery(TopicBean bean,QueryCondition qc){
		if(bean!=null){
			if(StringUtils.isNotBlank(bean.getTopicName())){
				qc.add("And obj.topicName like ? ","%"+ bean.getTopicName()+"%");
			}
			if(bean.getCreateTime() != null){
					qc.add("And obj.createTime = ? ", bean.getCreateTime());
				}
            if(bean.getEndTime() != null){
				qc.add("And obj.endTime = ? ", bean.getEndTime());
			}
            if(StringUtils.isNotBlank(bean.getTopicStatus())){
				qc.add("And obj.topicStatus = ? ", bean.getTopicStatus());
			}
            if(StringUtils.isNotBlank(bean.getKeywords())){
				qc.add("And obj.keywords like ? ", "%"+bean.getKeywords()+"%");
			}
            if(bean.getIsEnable() != null){
				qc.add("And obj.isEnable = ? ", bean.getIsEnable());
			}
            if(StringUtils.isNotBlank(bean.getSite().getSiteId())){
            	qc.add("And obj.site.siteId = ? ", bean.getSite().getSiteId());
            }
		}
	}

	/**
	 * 检查同一站点下专题名称唯一性
	 */
	public Boolean checkNameUnique(String topicName,String tname,String siteId){
		boolean unique=false;
		List<Topic> topicList=null;
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(topicName)){
			qc.add(" And obj.topicName =?", topicName);
		}
		if(StringUtils.isNotBlank(siteId)){
			qc.add(" And obj.site.siteId =?", siteId);
		}
		topicList=findByQc(qc);
		if(StringUtils.isNotBlank(tname) && !topicList.isEmpty()){
			for(int i=0;i<topicList.size();i++){
				if(topicList.get(i).getTopicName().equals(tname)){
					topicList.remove(i);
				}
			}
		}
		if((topicList!=null)&&(!topicList.isEmpty())){
			unique=true;
		}
		return unique;
	}
	/*
	 * 专题简称唯一验证
	 */
	public Boolean checkSimplenameunique(String simName, String siteId,String oldSimpleName){
		Boolean unique=false;
		List<Topic> simpleList=null;
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(simName)){
			qc.add("And obj.topicSimpleName =?" , simName);
		}
		if(StringUtils.isNotBlank(siteId)){
			qc.add("And obj.site.siteId=?", siteId);
		}
		simpleList=findByQc(qc);
		if(StringUtils.isNotBlank(oldSimpleName)&&(!simpleList.isEmpty())){
			for(int i=0;i<simpleList.size();i++){
				simpleList.remove(i);
			}
		}
		if(simpleList!=null&&(!simpleList.isEmpty())){
			unique=true;
		}
		return  unique;
	}

	public List<Topic> ftlFindTopics(String[] siteIds, String[] topicIds,
			long[] limit) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(!ArrayUtils.isEmpty(siteIds) && !ArrayUtils.isEmpty(topicIds)){
			qc.append(" And ("); 
			qc.append(" obj.site.siteId in "+ HqlUtil.arraysToString(siteIds));
			qc.append(" or obj.topicId in "+ HqlUtil.arraysToString(topicIds));
			qc.append(" ) "); 
		}else if(ArrayUtils.isEmpty(siteIds) && !ArrayUtils.isEmpty(topicIds)){
			qc.append(" And obj.topicId in "+ HqlUtil.arraysToString(topicIds));
		}else if(!ArrayUtils.isEmpty(siteIds) && ArrayUtils.isEmpty(topicIds)){
			qc.append(" And obj.site.siteId in "+ HqlUtil.arraysToString(siteIds));
		}
		
		if (!ArrayUtils.isEmpty(limit) && limit.length < 3) {
			if (limit.length == 1) {

				qc.setMaxResults((int) limit[0]);
				qc.setOffset(0);
			}
			if (limit.length == 2) {
				qc.setMaxResults((int) limit[1]);
				qc.setOffset((int) limit[0]);
			}
		}
		qc.append("And obj.topicStatus  in ('"
				+ DictionaryStaticValues.TOPIC_STATUS_SUCCESS + "','"
				+ DictionaryStaticValues.TOPIC_STATUS_PUBLISH + "')");
		qc.appenOrderBy(" obj.createTime desc");
		return findByQc(qc);
	}
}